package com.dhcc.sds.standard.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.cdc.busi.dao.CdcBsEmrStructMapper;
import com.dhcc.cdc.busi.dao.CdcLabItemMapper;
import com.dhcc.cdc.busi.dao.CdcLabOrderMapper;
import com.dhcc.cdc.busi.dao.CdcOrdmastMapper;
import com.dhcc.cdc.busi.entity.*;
import com.dhcc.cdc.busi.service.ICdcBsEmrStructService;
import com.dhcc.cdc.busi.service.ICdcDictionaryService;
import com.dhcc.cdc.sys.service.IDictionaryService;
import com.dhcc.core.framework.base.service.impl.BaseServiceImpl;
import com.dhcc.core.framework.security.ShiroKit;
import com.dhcc.core.framework.util.CommonUtil;
import com.dhcc.core.modules.system.entity.User;
import com.dhcc.sdc.cdss.dao.SdcCdssItemMatchMapper;
import com.dhcc.sdc.cdss.entity.SdcCdssItemEntity;
import com.dhcc.sdc.cdss.entity.SdcCdssItemMatchEntity;
import com.dhcc.sds.standard.dao.SdsStandDicMapper;
import com.dhcc.sds.standard.dao.SdsStandDicMatchMapper;
import com.dhcc.sds.standard.dao.SdsStandDicMatchParamMapper;
import com.dhcc.sds.standard.dto.CdcLabOrderDto;
import com.dhcc.sds.standard.dto.CdcLabitemDto;
import com.dhcc.sds.standard.dto.CdcOrdmastDto;
import com.dhcc.sds.standard.entity.*;
import com.dhcc.sds.standard.service.ISdsStandDicMatchParamService;
import com.dhcc.sds.standard.service.ISdsStandDicMatchService;
import com.dhcc.sds.standard.service.ISdsStdDicMatchLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@Service("sdsStandDicMatchService")
public class SdsStandDicMatchServiceImpl extends BaseServiceImpl<SdsStandDicMatchMapper, SdsStandDicMatchEntity> implements ISdsStandDicMatchService {

    @Resource
    private SdsStandDicMapper sdsStandDicMapper;
    @Resource
    private CdcOrdmastMapper cdcOrdmastMapper;
    @Autowired
    private ICdcDictionaryService cdcDictionaryService;
    @Resource
    private SdsStandDicMatchParamMapper standDicMatchParamMapper;
    @Resource
    private SdcCdssItemMatchMapper sdcCdssItemMatchMapper;
    @Autowired
    private IDictionaryService dictionaryService;
    @Autowired
    private ICdcBsEmrStructService cdcBsEmrStructService;
    @Resource
    private CdcLabOrderMapper cdcLabOrderMapper;
    @Resource
    private CdcLabItemMapper cdcLabItemMapper;
    @Autowired
    private ISdsStdDicMatchLogService sdsStdDicMatchLogService;


    @Override
    public List<SdsStandDicMatchEntity> page(Page<SdsStandDicMatchEntity> page, Map<String, Object> map) {
        return this.baseMapper.page(page, map);
    }

    @Override
    public List<SdsStandDicMatchEntity> list(Map<String, Object> map) {
        return this.baseMapper.list(map);
    }

    @Override
    public List<CdcOrdmastDto> pageMatchOrder(Page<SdsStandDicMatchEntity> page, Map<String, Object> params) {
        List<CdcOrdmastDto> cdcOrdmastDtoList = CollUtil.newArrayList();
        String id = (String) params.get("id");
        String orderKey = "";
        if (!StringUtils.isEmpty(params.get("orderKey"))){
            orderKey = (String) params.get("orderKey");
        }

        SdsStandDicEntity dicEntity = sdsStandDicMapper.selectById(Long.parseLong(id));
        if (!StringUtils.isEmpty(dicEntity)){
            params.put("dicId", dicEntity.getId());
        }else {
            return null;
        }
        //默认查询已对照的
        List<SdsStandDicMatchEntity> page1 = this.baseMapper.page(page, params);
        if (null != page1 && page1.size() > 0){
            for (SdsStandDicMatchEntity dicMatchEntity : page1){
                CdcOrdmastDto cdcOrdmastDto = new CdcOrdmastDto();
                CdcOrdmastEntity cdcOrdmastEntity = cdcOrdmastMapper.selectById(dicMatchEntity.getOrdmastId());
                if (!StringUtils.isEmpty(orderKey) && !cdcOrdmastEntity.getDescription().contains(orderKey)){
                    continue;
                }
                BeanUtil.copyProperties(cdcOrdmastEntity,cdcOrdmastDto);
                if (!StringUtils.isEmpty(cdcOrdmastEntity.getBusOrderCateId())){
                    CdcDictionaryEntity cdcDictionaryEntity = cdcDictionaryService.selectById(cdcOrdmastEntity.getBusOrderCateId());
                    if (!StringUtils.isEmpty(cdcDictionaryEntity)){
                        cdcOrdmastDto.setOrderTypeDesc(cdcDictionaryEntity.getDescription());
                    }
                    if (!StringUtils.isEmpty(cdcOrdmastEntity.getBusOrderSubcateId())){
                        CdcDictionaryEntity cdcDictionaryEntity2 = cdcDictionaryService.selectById(cdcOrdmastEntity.getBusOrderSubcateId());
                        if (!StringUtils.isEmpty(cdcDictionaryEntity2)){
                            cdcOrdmastDto.setOrderTypeDesc(cdcDictionaryEntity.getDescription() + "." + cdcDictionaryEntity2.getDescription());
                        }
                    }
                }
                cdcOrdmastDto.setChecked(1);
                cdcOrdmastDtoList.add(cdcOrdmastDto);
            }
        }

        return cdcOrdmastDtoList;
    }

    @Override
    public List<CdcOrdmastDto> pageOrder(Page<CdcOrdmastEntity> page, Map<String, Object> params) {
        List<CdcOrdmastDto> res = CollUtil.newArrayList();
        String compare = (String) params.get("compare");
        String id = (String) params.get("id");
        String orderKey = "";
        if (!StringUtils.isEmpty(params.get("orderKey"))){
            orderKey = (String) params.get("orderKey");
        }
        params.put("description", orderKey);
        SdsStandDicEntity dicEntity = sdsStandDicMapper.selectById(Long.parseLong(id));
        if (!StringUtils.isEmpty(dicEntity)){
            params.put("dicId", dicEntity.getId());
        }else {
            return null;
        }

        if (Objects.equals("all", compare)){
            //查询所有
            List<SdsStandDicMatchEntity> list = this.baseMapper.list(params);
            List<CdcOrdmastEntity> page1 = cdcOrdmastMapper.page(page, params);
            for (CdcOrdmastEntity cdcOrdmastEntity : page1){
                CdcOrdmastDto cdcOrdmastDto = new CdcOrdmastDto();
                BeanUtil.copyProperties(cdcOrdmastEntity,cdcOrdmastDto);
                if (!StringUtils.isEmpty(cdcOrdmastEntity.getBusOrderCateId())){
                    CdcDictionaryEntity cdcDictionaryEntity = cdcDictionaryService.selectById(cdcOrdmastEntity.getBusOrderCateId());
                    if (!StringUtils.isEmpty(cdcDictionaryEntity)){
                        cdcOrdmastDto.setOrderTypeDesc(cdcDictionaryEntity.getDescription());
                    }
                    if (!StringUtils.isEmpty(cdcOrdmastEntity.getBusOrderSubcateId())){
                        CdcDictionaryEntity cdcDictionaryEntity2 = cdcDictionaryService.selectById(cdcOrdmastEntity.getBusOrderSubcateId());
                        if (!StringUtils.isEmpty(cdcDictionaryEntity2)){
                            cdcOrdmastDto.setOrderTypeDesc(cdcDictionaryEntity.getDescription() + "." + cdcDictionaryEntity2.getDescription());
                        }
                    }
                }
                res.add(cdcOrdmastDto);
            }
            if (list.size() == 0){
                return res;
            }else {
                for (CdcOrdmastDto cdcOrdmastDto : res){
                    for (SdsStandDicMatchEntity dicMatchEntity : list) {
                        if (Objects.equals(cdcOrdmastDto.getId(), dicMatchEntity.getOrdmastId())) {
                            cdcOrdmastDto.setChecked(1);
                        }
                    }
                }
                return res;
            }
        }else if (Objects.equals("noMatch", compare)){
            //查询未对照的
            List<Long> matchOrdmastIds = this.baseMapper.selectMatchOrderIds(params);
            List<CdcOrdmastEntity> cdcOrdmastEntities = cdcOrdmastMapper.pageExcludeMatch(page, params, matchOrdmastIds);
            for (CdcOrdmastEntity cdcOrdmastEntity : cdcOrdmastEntities){
                CdcOrdmastDto cdcOrdmastDto = new CdcOrdmastDto();
                BeanUtil.copyProperties(cdcOrdmastEntity,cdcOrdmastDto);
                if (!StringUtils.isEmpty(cdcOrdmastEntity.getBusOrderCateId())){
                    CdcDictionaryEntity cdcDictionaryEntity = cdcDictionaryService.selectById(cdcOrdmastEntity.getBusOrderCateId());
                    if (!StringUtils.isEmpty(cdcDictionaryEntity)){
                        cdcOrdmastDto.setOrderTypeDesc(cdcDictionaryEntity.getDescription());
                    }
                    if (!StringUtils.isEmpty(cdcOrdmastEntity.getBusOrderSubcateId())){
                        CdcDictionaryEntity cdcDictionaryEntity2 = cdcDictionaryService.selectById(cdcOrdmastEntity.getBusOrderSubcateId());
                        if (!StringUtils.isEmpty(cdcDictionaryEntity2)){
                            cdcOrdmastDto.setOrderTypeDesc(cdcDictionaryEntity.getDescription() + "." + cdcDictionaryEntity2.getDescription());
                        }
                    }
                }
                res.add(cdcOrdmastDto);
            }
        }

        return res;
    }

    @Override
    public void compare(Long dicId, String orderIDStr, Integer matchType) {
        String[] split = orderIDStr.split(",");
        for (String s : split) {
            SdsStandDicMatchEntity sdsStandDicMatchEntity = new SdsStandDicMatchEntity();
            sdsStandDicMatchEntity.setDicId(dicId);
            sdsStandDicMatchEntity.setMatchType(matchType);
            if (1 == matchType){
                sdsStandDicMatchEntity.setOrdmastId(Long.parseLong(s));
            }else if (2 == matchType){
                sdsStandDicMatchEntity.setOrdmastId(Long.parseLong(s));
            }else if (3 == matchType){
                sdsStandDicMatchEntity.setSubTitle(s);
            }else if (4 == matchType){
                sdsStandDicMatchEntity.setLabItemId(Long.parseLong(s));
            }
            SdsStandDicMatchEntity dicMatchEntity = this.baseMapper.selectOne(sdsStandDicMatchEntity);
            if (StringUtils.isEmpty(dicMatchEntity)) {
                sdsStandDicMatchEntity.setOperDate(new Date());
                this.baseMapper.insert(sdsStandDicMatchEntity);
            }
            this.saveMatchLog(dicId, s, matchType,1);
        }
    }
    private void saveMatchLog(Long dicId, String match, Integer matchType,int opeType){
        String execUser = "";
        if (ShiroKit.getUser()!=null) {
            User user = ShiroKit.getUser();
            execUser = user.getLoginName();
        }
            SdsStdDicMatchLogEntity log = new SdsStdDicMatchLogEntity();
            log.setMatchType(matchType.toString());
            log.setDicId(dicId);
            log.setOperType(opeType);
            //1 对照医嘱 2.对照检验医嘱 3 对照病历子标题 4对照检验项目
            if(1 == matchType){
                log.setOrdmastId(Long.parseLong(match));
            } else if(2 == matchType){
                log.setLaborderId(Long.parseLong(match));
            } else if(3 == matchType){
                log.setEmrSubTitle(match);
            } else if(4 == matchType){
                log.setLabitemId(Long.parseLong(match));
            }
            log.setOperTime(new Date());
            log.setOperUser(execUser);
            sdsStdDicMatchLogService.insert(log);
    }

    @Override
    public void undo(Long dicId, String orderIDStr, Integer matchType) {
        String[] split = orderIDStr.split(",");
        for (String s : split){
            SdsStandDicMatchEntity sdsStandDicMatchEntity = new SdsStandDicMatchEntity();
            sdsStandDicMatchEntity.setDicId(dicId);
            sdsStandDicMatchEntity.setMatchType(matchType);
            if (1 == matchType){
                sdsStandDicMatchEntity.setOrdmastId(Long.parseLong(s));
            }else if (2 == matchType){
                sdsStandDicMatchEntity.setOrdmastId(Long.parseLong(s));
            }else if (3 == matchType){
                sdsStandDicMatchEntity.setSubTitle(s);
            }else if (4 == matchType){
                sdsStandDicMatchEntity.setLabItemId(Long.parseLong(s));
            }
            SdsStandDicMatchEntity dicMatchEntity = this.baseMapper.selectOne(sdsStandDicMatchEntity);
            this.baseMapper.deleteById(dicMatchEntity.getId());
            this.saveMatchLog(dicId, s, matchType,2);
        }
    }

    @Override
    public List<CdcLabOrderDto> pageLisOrder(Page<CdcLabOrderEntity> page, Map<String, Object> params) {
        List<CdcLabOrderDto> res = CollUtil.newArrayList();
        String compare = (String) params.get("compare");
        String id = (String) params.get("id");
        String orderKey = "";
        if (!StringUtils.isEmpty(params.get("orderKey"))){
            orderKey = (String) params.get("orderKey");
        }
        params.put("description", orderKey);
        SdsStandDicEntity dicEntity = sdsStandDicMapper.selectById(Long.parseLong(id));
        if (!StringUtils.isEmpty(dicEntity)){
            params.put("dicId", dicEntity.getId());
        }else {
            return null;
        }

        if (Objects.equals("all", compare)){
            //查询所有
            List<SdsStandDicMatchEntity> list = this.baseMapper.list(params);
            List<CdcLabOrderEntity> page1 = cdcLabOrderMapper.page(page, params);
            for (CdcLabOrderEntity cdcLabOrderEntity : page1){
                CdcLabOrderDto cdcLabOrderDto = new CdcLabOrderDto();
                BeanUtil.copyProperties(cdcLabOrderEntity,cdcLabOrderDto);
                if (!StringUtils.isEmpty(cdcLabOrderEntity.getOrderMastId())){
                    CdcOrdmastEntity cdcOrdmastEntity = cdcOrdmastMapper.selectById(cdcLabOrderEntity.getOrderMastId());
                    if (!StringUtils.isEmpty(cdcOrdmastEntity)){
                        cdcLabOrderDto.setOrderMastDesc(cdcOrdmastEntity.getDescription());
                    }
                }
                res.add(cdcLabOrderDto);
            }
            if (list.size() == 0){
                return res;
            }else {
                for (CdcLabOrderDto cdcLabOrderDto : res){
                    for (SdsStandDicMatchEntity dicMatchEntity : list) {
                        if (Objects.equals(cdcLabOrderDto.getId(), dicMatchEntity.getOrdmastId())) {
                            cdcLabOrderDto.setChecked(1);
                        }
                    }
                }
                return res;
            }
        }else if (Objects.equals("noMatch", compare)){
            //查询未对照的
            List<Long> matchLabOrdIds = this.baseMapper.selectMatchOrderIds(params);
            List<CdcLabOrderEntity> cdcLabOrderEntities = cdcLabOrderMapper.pageExcludeMatch(page, params, matchLabOrdIds);
            for (CdcLabOrderEntity cdcLabOrderEntity : cdcLabOrderEntities){
                CdcLabOrderDto cdcLabOrderDto = new CdcLabOrderDto();
                BeanUtil.copyProperties(cdcLabOrderEntity,cdcLabOrderDto);
                if (!StringUtils.isEmpty(cdcLabOrderEntity.getOrderMastId())){
                    CdcOrdmastEntity cdcOrdmastEntity = cdcOrdmastMapper.selectById(cdcLabOrderEntity.getOrderMastId());
                    if (!StringUtils.isEmpty(cdcOrdmastEntity)){
                        cdcLabOrderDto.setOrderMastDesc(cdcOrdmastEntity.getDescription());
                    }
                }
                res.add(cdcLabOrderDto);
            }
        }

        return res;
    }

    @Override
    public List<CdcLabOrderDto> pageMatchLisOrder(Page<SdsStandDicMatchEntity> page, Map<String, Object> params) {
        List<CdcLabOrderDto> res = CollUtil.newArrayList();
        String id = (String) params.get("id");
        String orderKey = "";
        if (!StringUtils.isEmpty(params.get("orderKey"))){
            orderKey = (String) params.get("orderKey");
        }

        SdsStandDicEntity dicEntity = sdsStandDicMapper.selectById(Long.parseLong(id));
        if (!StringUtils.isEmpty(dicEntity)){
            params.put("dicId", dicEntity.getId());
        }else {
            return null;
        }
        //默认查询已对照的
        List<SdsStandDicMatchEntity> page1 = this.baseMapper.page(page, params);
        if (null != page1 && page1.size() > 0){
            for (SdsStandDicMatchEntity dicMatchEntity : page1){
                CdcLabOrderDto cdcLabOrderDto = new CdcLabOrderDto();
                CdcLabOrderEntity cdcLabOrderEntity = cdcLabOrderMapper.selectById(dicMatchEntity.getOrdmastId());
                if (!StringUtils.isEmpty(orderKey) && !cdcLabOrderEntity.getDescription().contains(orderKey)){
                    continue;
                }
                BeanUtil.copyProperties(cdcLabOrderEntity,cdcLabOrderDto);
                if (!StringUtils.isEmpty(cdcLabOrderEntity.getOrderMastId())){
                    CdcOrdmastEntity cdcOrdmastEntity = cdcOrdmastMapper.selectById(cdcLabOrderEntity.getOrderMastId());
                    if (!StringUtils.isEmpty(cdcOrdmastEntity)){
                        cdcLabOrderDto.setOrderMastDesc(cdcOrdmastEntity.getDescription());
                    }
                }
                cdcLabOrderDto.setChecked(1);
                res.add(cdcLabOrderDto);
            }
        }

        return res;
    }

    @Override
    public List<CdcOrdmastDto> pageMatchEmr(Page<SdsStandDicMatchEntity> page, Map<String, Object> params) {
        List<CdcOrdmastDto> cdcOrdmastDtoList = CollUtil.newArrayList();
        String id = (String) params.get("id");
        String orderKey = "";
        if (!StringUtils.isEmpty(params.get("orderKey"))){
            orderKey = (String) params.get("orderKey");
        }

        SdsStandDicEntity dicEntity = sdsStandDicMapper.selectById(Long.parseLong(id));
        if (!StringUtils.isEmpty(dicEntity)){
            params.put("dicId", dicEntity.getId());
        }else {
            return null;
        }
        //默认查询已对照的
        List<SdsStandDicMatchEntity> page1 = this.baseMapper.page(page, params);
        if (null != page1 && page1.size() > 0){
            for (SdsStandDicMatchEntity dicMatchEntity : page1){
                CdcOrdmastDto cdcOrdmastDto = new CdcOrdmastDto();
                String subTitle = dicMatchEntity.getSubTitle();
                if (!StringUtils.isEmpty(orderKey) && !subTitle.contains(orderKey)){
                    continue;
                }
                cdcOrdmastDto.setDescription(subTitle);
                cdcOrdmastDto.setChecked(1);
                cdcOrdmastDtoList.add(cdcOrdmastDto);
            }
        }

        return cdcOrdmastDtoList;
    }

    @Override
    public List<CdcOrdmastDto> pageEmr(Page<CdcOrdmastDto> page, Map<String, Object> params) {
        List<CdcOrdmastDto> res = CollUtil.newArrayList();
        String compare = (String) params.get("compare");
        String id = (String) params.get("id");
        String orderKey = "";
        if (!StringUtils.isEmpty(params.get("orderKey"))){
            orderKey = (String) params.get("orderKey");
        }
        params.put("description", orderKey);
        SdsStandDicEntity dicEntity = sdsStandDicMapper.selectById(Long.parseLong(id));
        if (!StringUtils.isEmpty(dicEntity)){
            params.put("dicId", dicEntity.getId());
        }else {
            return null;
        }

        if (Objects.equals("all", compare)){
            //查询所有
            List<SdsStandDicMatchEntity> list = this.baseMapper.list(params);
            List<String> page1 = this.baseMapper.selectEmrSubTitle(params);
            for (String s : page1){
                CdcOrdmastDto cdcOrdmastDto = new CdcOrdmastDto();
                cdcOrdmastDto.setDescription(s);
                res.add(cdcOrdmastDto);
            }
            if (list.size() == 0){
                return res;
            }else {
                for (CdcOrdmastDto cdcOrdmastDto : res){
                    for (SdsStandDicMatchEntity dicMatchEntity : list) {
                        if (Objects.equals(cdcOrdmastDto.getDescription(), dicMatchEntity.getSubTitle())) {
                            cdcOrdmastDto.setChecked(1);
                        }
                    }
                }
                return res;
            }
        }else if (Objects.equals("noMatch", compare)){
            //查询未对照的
            List<String> emrSubTitle = this.baseMapper.selectExcludeMatchEmrSubTitle(params);
            for (String s : emrSubTitle){
                CdcOrdmastDto cdcOrdmastDto = new CdcOrdmastDto();
                cdcOrdmastDto.setDescription(s);
                res.add(cdcOrdmastDto);
            }
        }

        return res;
    }

    @Override
    public void autoCompare(Long dicId, String dicName, Integer matchType) {


        Map<Long, List<SdsStandDicMatchParamEntity>> map = new HashMap<>();
        if ("".equals(dicName)){
            List<SdsStandDicMatchParamEntity> sdsStandDicMatchParamEntities = standDicMatchParamMapper.selectList(null);
            for (SdsStandDicMatchParamEntity sdsStandDicMatchParamEntity : sdsStandDicMatchParamEntities){
                List<SdsStandDicMatchParamEntity> dicMatchParamEntities = new ArrayList<>();
                if (StringUtils.isEmpty(map.get(sdsStandDicMatchParamEntity.getDicId()))){
                    dicMatchParamEntities.add(sdsStandDicMatchParamEntity);
                    map.put(sdsStandDicMatchParamEntity.getDicId(), dicMatchParamEntities);
                }else {
                    List<SdsStandDicMatchParamEntity> dicMatchParamEntities1 = map.get(sdsStandDicMatchParamEntity.getDicId());
                    dicMatchParamEntities1.add(sdsStandDicMatchParamEntity);
                    map.put(sdsStandDicMatchParamEntity.getDicId(), dicMatchParamEntities1);
                }
            }
        }else {
            List<SdsStandDicEntity> dicEntities = sdsStandDicMapper.selectList(null);
            List<SdsStandDicEntity> children = getChildren(dicEntities, dicId);
            List<SdsStandDicMatchParamEntity> dicMatchParamEntities = standDicMatchParamMapper.selectList(new EntityWrapper<SdsStandDicMatchParamEntity>()
                    .eq("dic_id", dicId));
            if (null != dicMatchParamEntities && dicMatchParamEntities.size() > 0){
                map.put(dicId, dicMatchParamEntities);
            }
            getDicMatchParam(map, children);

        }

        for (Map.Entry<Long, List<SdsStandDicMatchParamEntity>> entry : map.entrySet()){
            SdsStandDicEntity dicEntity = sdsStandDicMapper.selectById(entry.getKey());
            if (null == dicEntity){
                continue;
            }

            //排除关键字sql
            String kfilterTxt = "";
            //排除通用名sql
            String cfilterTxt = "";
            //同时满足关键字sql
            String kwordAndTxt = "";
            //同时满足通用名sql
            String cwordAndTxt = "";
            //满足一个关键字sql
            String kwordOrTxt = "";
            //满足一个通用名sql
            String cwordOrTxt = "";

            List<SdsStandDicMatchParamEntity> value = entry.getValue();

            for (SdsStandDicMatchParamEntity sdsStandDicMatchParam : value){
                if ("O".equals(sdsStandDicMatchParam.getFilterType())){
                    //O-排除 排除只处理对照内容只有一个的情况（包含&未考虑
                    if ("K".equals(sdsStandDicMatchParam.getCompareType())){
                        kfilterTxt = kfilterTxt + "|" + sdsStandDicMatchParam.getCompareContent();
                    }else if ("C".equals(sdsStandDicMatchParam.getCompareType())){
                        cfilterTxt = cfilterTxt + "|" + sdsStandDicMatchParam.getCompareContent();
                    }
                }else if ("I".equals(sdsStandDicMatchParam.getFilterType())){
                    if ("K".equals(sdsStandDicMatchParam.getCompareType())){
                        if (sdsStandDicMatchParam.getCompareContent().contains("&")){
                            String[] split1 = sdsStandDicMatchParam.getCompareContent().split("&");
                            for (int i = 0; i <= split1.length - 1; i++) {
                                if (i == 0) {
                                    kwordAndTxt = kwordAndTxt + " or description like '%" + split1[0] + "%'";
                                } else {
                                    kwordAndTxt = kwordAndTxt + " and description like '%" + split1[i] + "%'";
                                }
                            }
                        }else {
                            kwordOrTxt = kwordOrTxt + "|" +sdsStandDicMatchParam.getCompareContent();
                        }
                    }else if ("C".equals(sdsStandDicMatchParam.getCompareType())){
                        if (sdsStandDicMatchParam.getCompareContent().contains("&")){
                            String[] split2 = sdsStandDicMatchParam.getCompareContent().split("&");
                            for (int i = 0; i <= split2.length - 1; i++) {
                                if (i == 0) {
                                    cwordAndTxt = cwordAndTxt + " or drug_common_name like '%" + split2[0] + "%'";
                                } else {
                                    cwordAndTxt = cwordAndTxt + " and drug_common_name like '%" + split2[i] + "%'";
                                }
                            }
                        }else {
                            cwordOrTxt = cwordOrTxt + "|" +sdsStandDicMatchParam.getCompareContent();
                        }
                    }
                }
            }

            List<Long> matchIds = new ArrayList<>();
            Integer compareType = dicEntity.getCompareType();
            List<SdsStandDicMatchEntity> standDicMatchEntities = new ArrayList<>();
            if ("" != kwordOrTxt) {
                kwordOrTxt = kwordOrTxt.substring(1, kwordOrTxt.length());

            }
            if ("" != kwordAndTxt){
                if (3 == compareType){
                    kwordAndTxt = kwordAndTxt.replace("description", "sub_title");
                }
            }
            if ("" != cwordOrTxt) {
                cwordOrTxt = cwordOrTxt.substring(1, cwordOrTxt.length());
            }
            if ("" != kfilterTxt) {
                kfilterTxt = kfilterTxt.substring(1, kfilterTxt.length());
            }
            if ("" != cfilterTxt) {
                cfilterTxt = cfilterTxt.substring(1, cfilterTxt.length());
            }
            if ("" != kwordAndTxt && ("" == kwordOrTxt || "" == cwordOrTxt)) {
                kwordAndTxt = kwordAndTxt.replaceFirst("or", "and");
            }
            if ("" != cwordAndTxt && ("" == kwordOrTxt || "" == cwordOrTxt)) {
                cwordAndTxt = cwordAndTxt.replaceFirst("or", "and");
            }

            if (1 == compareType) {
                //医嘱
                matchIds = this.baseMapper.compareOrderByRegexp(kfilterTxt, cfilterTxt, kwordAndTxt, cwordAndTxt, kwordOrTxt, cwordOrTxt, null);
                for (Long matchId : matchIds) {
                    SdsStandDicMatchEntity sdsStandDicMatchEntity = new SdsStandDicMatchEntity();
                    sdsStandDicMatchEntity.setOrdmastId(matchId);
                    sdsStandDicMatchEntity.setMatchType(compareType);
                    sdsStandDicMatchEntity.setDicId(dicEntity.getId());
                    SdsStandDicMatchEntity sdsStandDicMatchEntity1 = this.baseMapper.selectOne(sdsStandDicMatchEntity);
                    if (StringUtils.isEmpty(sdsStandDicMatchEntity1)) {
                        standDicMatchEntities.add(sdsStandDicMatchEntity);
                    }
                }
            } else if (2 == compareType) {
                //检验医嘱
                matchIds = this.baseMapper.compareLabOrdByRegexp(kfilterTxt, kwordAndTxt, kwordOrTxt);
                for (Long matchId : matchIds) {
                    SdsStandDicMatchEntity sdsStandDicMatchEntity = new SdsStandDicMatchEntity();
                    sdsStandDicMatchEntity.setOrdmastId(matchId);
                    sdsStandDicMatchEntity.setMatchType(compareType);
                    sdsStandDicMatchEntity.setDicId(dicEntity.getId());
                    SdsStandDicMatchEntity sdsStandDicMatchEntity1 = this.baseMapper.selectOne(sdsStandDicMatchEntity);
                    if (StringUtils.isEmpty(sdsStandDicMatchEntity1)) {
                        standDicMatchEntities.add(sdsStandDicMatchEntity);
                    }
                }
            }else if (3 == compareType){
                //病历子标题
                List<String> strings = this.baseMapper.compareEmrByRegexp(kfilterTxt, kwordAndTxt, kwordOrTxt);
                for (String s : strings){
                    SdsStandDicMatchEntity sdsStandDicMatchEntity = new SdsStandDicMatchEntity();
                    sdsStandDicMatchEntity.setSubTitle(s);
                    sdsStandDicMatchEntity.setMatchType(compareType);
                    sdsStandDicMatchEntity.setDicId(dicEntity.getId());
                    SdsStandDicMatchEntity sdsStandDicMatchEntity1 = this.baseMapper.selectOne(sdsStandDicMatchEntity);
                    if (StringUtils.isEmpty(sdsStandDicMatchEntity1)) {
                        standDicMatchEntities.add(sdsStandDicMatchEntity);
                    }
                }
            }else if (4 == compareType){
                //检验项目
                matchIds = this.baseMapper.compareLabItemByRegexp(kfilterTxt, kwordAndTxt, kwordOrTxt);
                for (Long matchId : matchIds) {
                    SdsStandDicMatchEntity sdsStandDicMatchEntity = new SdsStandDicMatchEntity();
                    sdsStandDicMatchEntity.setLabItemId(matchId);
                    sdsStandDicMatchEntity.setMatchType(compareType);
                    sdsStandDicMatchEntity.setDicId(dicEntity.getId());
                    SdsStandDicMatchEntity sdsStandDicMatchEntity1 = this.baseMapper.selectOne(sdsStandDicMatchEntity);
                    if (StringUtils.isEmpty(sdsStandDicMatchEntity1)) {
                        standDicMatchEntities.add(sdsStandDicMatchEntity);
                    }
                }
            }

            if (null != standDicMatchEntities && standDicMatchEntities.size() > 0) {
                this.baseMapper.insertBatch(standDicMatchEntities);
            }
        }

    }

    @Override
    public List<CdcLabitemDto> pageMatchLisItem(Page<SdsStandDicMatchEntity> page, Map<String, Object> params) {
        List<CdcLabitemDto> res = CollUtil.newArrayList();
        String id = (String) params.get("id");
        String orderKey = "";
        if (!StringUtils.isEmpty(params.get("orderKey"))){
            orderKey = (String) params.get("orderKey");
        }

        SdsStandDicEntity dicEntity = sdsStandDicMapper.selectById(Long.parseLong(id));
        if (!StringUtils.isEmpty(dicEntity)){
            params.put("dicId", dicEntity.getId());
        }else {
            return null;
        }
        //默认查询已对照的
        List<SdsStandDicMatchEntity> page1 = this.baseMapper.page(page, params);
        Map map = new HashMap();

        if (null != page1 && page1.size() > 0){
            for (SdsStandDicMatchEntity dicMatchEntity : page1){
                CdcLabitemDto cdcLabitemDto = new CdcLabitemDto();
                map.clear();
                map.put("itemId",dicMatchEntity.getLabItemId());
                List<CdcLabItemEntity> cdcLabItemEntitys = cdcLabItemMapper.list(map);
                if(CommonUtil.isNotEmpty(cdcLabItemEntitys)&&cdcLabItemEntitys.size()>0){
                    if (!StringUtils.isEmpty(orderKey) && !cdcLabItemEntitys.get(0).getDescription().contains(orderKey)){
                        continue;
                    }
                    BeanUtil.copyProperties(cdcLabItemEntitys.get(0),cdcLabitemDto);
                    cdcLabitemDto.setLabSetDesc(cdcLabItemEntitys.get(0).getOrderDesc());
                    cdcLabitemDto.setChecked(1);
                    res.add(cdcLabitemDto);
                }
//                CdcLabItemEntity cdcLabItemEntity = cdcLabItemMapper.selectById(dicMatchEntity.getLabItemId());
//                if (!StringUtils.isEmpty(orderKey) && !cdcLabItemEntity.getDescription().contains(orderKey)){
//                    continue;
//                }

            }
        }

        return res;
    }

    @Override
    public List<CdcLabitemDto> pageLisItem(Page<CdcLabItemEntity> page, Map<String, Object> params) {
        List<CdcLabitemDto> res = CollUtil.newArrayList();
        String compare = (String) params.get("compare");
        String id = (String) params.get("id");
        String orderKey = "";
        if (!StringUtils.isEmpty(params.get("orderKey"))){
            orderKey = (String) params.get("orderKey");
        }
        params.put("description", orderKey);
        SdsStandDicEntity dicEntity = sdsStandDicMapper.selectById(Long.parseLong(id));
        if (!StringUtils.isEmpty(dicEntity)){
            params.put("dicId", dicEntity.getId());
            // 查询父节点已对照的检验医嘱
            //默认查询已对照的
            Map pParam = new HashMap();
            pParam.put("dicId", dicEntity.getParentId());
            List<SdsStandDicMatchEntity> labOrderList = this.baseMapper.list(pParam);
            List<Long> laborderIds = new ArrayList<>();
            for (SdsStandDicMatchEntity sdsStandDicMatchEntity : labOrderList){
                Long laborderId = sdsStandDicMatchEntity.getOrdmastId();
                laborderIds.add(laborderId);
            }
            if (null == laborderIds || laborderIds.size() ==0){
                return null;
            }
            params.put("laborderIds", laborderIds);
        }else {
            return null;
        }

        if (Objects.equals("all", compare)){
            //查询所有
            List<SdsStandDicMatchEntity> list = this.baseMapper.list(params);
            List<CdcLabItemEntity> page1 = cdcLabItemMapper.findPage(page, params);
            for (CdcLabItemEntity cdcLabItemEntity : page1){
                CdcLabitemDto cdcLabitemDto = new CdcLabitemDto();
                BeanUtil.copyProperties(cdcLabItemEntity,cdcLabitemDto);
                cdcLabitemDto.setLabSetDesc(cdcLabItemEntity.getOrderDesc());
                res.add(cdcLabitemDto);
            }
            if (list.size() == 0){
                return res;
            }else {
                for (CdcLabitemDto cdcLabitemDto : res){
                    for (SdsStandDicMatchEntity dicMatchEntity : list) {
                        if (Objects.equals(cdcLabitemDto.getId(), dicMatchEntity.getLabItemId())) {
                            cdcLabitemDto.setChecked(1);
                        }
                    }
                }
                return res;
            }
        }else if (Objects.equals("noMatch", compare)){
            //查询未对照的
            List<Long> matchLabItemIds = this.baseMapper.selectMatchLabIds(params);
            List<CdcLabItemEntity> cdcLabItemEntities = cdcLabItemMapper.pageExcludeMatch(page, params, matchLabItemIds);
            for (CdcLabItemEntity cdcLabItemEntity : cdcLabItemEntities){
                CdcLabitemDto cdcLabitemDto = new CdcLabitemDto();
                BeanUtil.copyProperties(cdcLabItemEntity,cdcLabitemDto);
                cdcLabitemDto.setLabSetDesc(cdcLabItemEntity.getOrderDesc());
                res.add(cdcLabitemDto);
            }
        }

        return res;
    }


    private List<String> handleRegexp(String[] split) {
        List<String> res = new ArrayList<>();
        //排除sql
        String filterTxt = "";
        //同时满足sql
        String keywordAndTxt = "";
        //满足一个sql
        String keywordOrTxt = "";
        for (String s1 : split) {
            if (s1.contains("!")) {
                filterTxt = filterTxt + "|" + s1.substring(1, s1.length());
            } else if (s1.contains("&")) {
                //同时满足
                String[] split1 = s1.split("&");
                for (int i = 0; i <= split1.length - 1; i++) {
                    if (i == 0) {
                        keywordAndTxt = keywordAndTxt + " or description like '%" + split1[0] + "%'";
                    } else {
                        keywordAndTxt = keywordAndTxt + " and description like '%" + split1[i] + "%'";
                    }
                }
                //keywordAndTxt = keywordAndTxt + " or description like '%" + split1[0] + "%' and description like '%" + split1[1] + "%'";
            } else {
                keywordOrTxt = keywordOrTxt + "|" + s1;
            }
        }
        if ("" != filterTxt) {
            filterTxt = filterTxt.substring(1, filterTxt.length());
        }
        if ("" != keywordOrTxt) {
            keywordOrTxt = keywordOrTxt.substring(1, keywordOrTxt.length());
        }
        if ("" != keywordAndTxt && "" == keywordOrTxt) {
            keywordAndTxt = keywordAndTxt.replaceFirst("or", "and");
        }
        res.add(filterTxt);
        res.add(keywordAndTxt);
        res.add(keywordOrTxt);
        return res;
    }

    /**
     * 获取子分类
     *
     * @param dicEntities
     * @return
     */
    private List<SdsStandDicEntity> getChildren(List<SdsStandDicEntity> dicEntities, Long parentId) {
        return dicEntities.stream()
                .filter(l -> l.getParentId().equals(parentId))
                .peek(c -> c.setChildren(getChildren(dicEntities, c.getId())))
                .sorted(Comparator.comparingLong(SdsStandDicEntity::getId))
                .collect(Collectors.toList());

    }

    private Map<Long, List<SdsStandDicMatchParamEntity>> getDicMatchParam(Map<Long, List<SdsStandDicMatchParamEntity>> map, List<SdsStandDicEntity> dicEntities) {
        for (SdsStandDicEntity standDicEntity : dicEntities){
            List<SdsStandDicMatchParamEntity> dicMatchParamEntities1 = standDicMatchParamMapper.selectList(new EntityWrapper<SdsStandDicMatchParamEntity>()
                    .eq("dic_id", standDicEntity.getId()));
            if (null != dicMatchParamEntities1 && dicMatchParamEntities1.size() > 0){
                map.put(standDicEntity.getId(), dicMatchParamEntities1);
            }
            if (null != standDicEntity.getChildren() && standDicEntity.getChildren().size() > 0){
                getDicMatchParam(map, standDicEntity.getChildren());
            }
        }
        return map;
    }

}
